package com.senlingspace.system.service.impl;

import java.util.List;

import com.senlingspace.common.exception.UtilException;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.SecurityUtils;
import com.senlingspace.system.domain.SlkjShopOperate;
import com.senlingspace.system.domain.dto.FzShopProjectDto;
import com.senlingspace.system.domain.vo.SharesProjectVo;
import com.senlingspace.system.mapper.SlkjShopOperateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkjShopProjectFzMapper;
import com.senlingspace.system.domain.SlkjShopProjectFz;
import com.senlingspace.system.service.ISlkjShopProjectFzService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分账比例-门店项目关联Service业务层处理
 * 
 * @author wangliang
 * @date 2024-07-15
 */
@Service
public class SlkjShopProjectFzServiceImpl implements ISlkjShopProjectFzService 
{
    @Autowired
    private SlkjShopProjectFzMapper slkjShopProjectFzMapper;
    @Autowired
    private SlkjShopOperateMapper operateMapper;

    /**
     * 查询分账比例-门店项目关联
     * 
     * @param id 分账比例-门店项目关联主键
     * @return 分账比例-门店项目关联
     */
    @Override
    public SlkjShopProjectFz selectSlkjShopProjectFzById(Long id)
    {
        return slkjShopProjectFzMapper.selectSlkjShopProjectFzById(id);
    }

    /**
     * 查询分账比例-门店项目关联列表
     * 
     * @param slkjShopProjectFz 分账比例-门店项目关联
     * @return 分账比例-门店项目关联
     */
    @Override
    public List<SlkjShopProjectFz> selectSlkjShopProjectFzList(SlkjShopProjectFz slkjShopProjectFz)
    {
        return slkjShopProjectFzMapper.selectSlkjShopProjectFzList(slkjShopProjectFz);
    }

    /**
     * 新增分账比例-门店项目关联
     * 
     * @param projectFz 分账比例-门店项目关联
     * @return 结果
     */
    @Override
    public int insertSlkjShopProjectFz(SlkjShopProjectFz projectFz)
    {
        SlkjShopProjectFz shopProjectFz = slkjShopProjectFzMapper.selectShopProjectByProId(projectFz.getShopId());
        if(shopProjectFz != null){
            throw new IllegalArgumentException("门店只能有一个项目存在，请不要重复添加");
        }
        projectFz.setCreateBy(SecurityUtils.getUsername());
        return slkjShopProjectFzMapper.insertSlkjShopProjectFz(projectFz);
    }

    /**
     * 修改分账比例-门店项目关联
     *
     * @param slkjShopProjectFz 分账比例-门店项目关联
     * @return 结果
     */
    @Override
    public int updateSlkjShopProjectFz(SlkjShopProjectFz slkjShopProjectFz)
    {
        slkjShopProjectFz.setUpdateTime(DateUtils.getNowDate());
        return slkjShopProjectFzMapper.updateSlkjShopProjectFz(slkjShopProjectFz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteFzShopProjectById(FzShopProjectDto projectFz) {
        int projectNum = slkjShopProjectFzMapper.weakDeleteSlkjShopProjectFzById(projectFz.getId());

        SlkjShopOperate shopOperate = new SlkjShopOperate();
        shopOperate.setId(projectFz.getOperateId());
        shopOperate.setInvestorBefore(projectFz.getShares());
        shopOperate.setInvestorAfter(projectFz.getShares());
        shopOperate.setUpdateBy(SecurityUtils.getUsername());
        shopOperate.setUpdateTime(DateUtils.getNowDate());
        int operateNum = operateMapper.updateSlkjShopOperateAdd(shopOperate);
        if(operateNum == 0 || projectNum == 0 ){
            throw new UtilException("修改数据失败");
        }
        return projectNum;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateFzShopProjectById(FzShopProjectDto projectFz) {
        projectFz.setUpdateBy(SecurityUtils.getUsername());
        projectFz.setUpdateTime(DateUtils.getNowDate());
        int projectNum = slkjShopProjectFzMapper.updateFzShopProjectById(projectFz);
        SlkjShopOperate shopOperate = new SlkjShopOperate();
        shopOperate.setId(projectFz.getOperateId());
        shopOperate.setInvestorBefore(projectFz.getOperateBefore());
        shopOperate.setInvestorAfter(projectFz.getOperateAfter());
        shopOperate.setUpdateBy(SecurityUtils.getUsername());
        shopOperate.setUpdateTime(DateUtils.getNowDate());
        int operateNum = operateMapper.updateSlkjShopOperate(shopOperate);
        if(operateNum == 0 || projectNum == 0 ){
            throw new UtilException("修改数据失败");
        }
        return projectNum;
    }

    /**
     * 批量删除分账比例-门店项目关联
     * 
     * @param ids 需要删除的分账比例-门店项目关联主键
     * @return 结果
     */
    @Override
    public int deleteSlkjShopProjectFzByIds(Long[] ids)
    {
        return slkjShopProjectFzMapper.deleteSlkjShopProjectFzByIds(ids);
    }

    /**
     * 删除分账比例-门店项目关联信息
     * 
     * @param id 分账比例-门店项目关联主键
     * @return 结果
     */
    @Override
    public int deleteSlkjShopProjectFzById(Long id)
    {
        return slkjShopProjectFzMapper.deleteSlkjShopProjectFzById(id);
    }

    @Override
    public List<SharesProjectVo> selectProjectShopByShopId(Long shopId) {

        return slkjShopProjectFzMapper.selectProjectShopByShopId(shopId);
    }
}
